﻿#include "mylabel_p.h"

MyLabelPrivate::MyLabelPrivate()
    : imageLayerIndex(1),
      deviceIndex(1),
      objectType(My::M),
      objectIndex(0),
      objectData(0),
      borderColor(Qt::black),
      borderWidth(1),
      borderRadius(5),
      myLabelBgColor(Qt::white),
      displayEnable(true),
      displayDefault(true),
      defaultText("Label"),
      textBuffer(),
      LeftmousePressed(false),
      mousePressPoint(0, 0)
{
}

MyLabelPrivate::~MyLabelPrivate()
{
}

MyLabel::MyLabel(QWidget *parent, Qt::WindowFlags f)
    : QLabel(parent, f),
      d(new MyLabelPrivate)
{
    this->setText(d->defaultText);
}

MyLabel::~MyLabel()
{
    delete d;
}

/*!
    \property MyLabel::imageLayerIndex
    \brief the index of the imageLayer

    The imageLayer is the layer of the widget.

    The default imageLayerIndex is 1.

    \sa indent
*/
int MyLabel::getImageLayerIndex() const
{
    return d->imageLayerIndex;
}

void MyLabel::setImageLayerIndex(int imageLayerInd)
{
    if(d->imageLayerIndex == imageLayerInd)
        return;
    d->imageLayerIndex = imageLayerInd;
}

/*!
    \property MyLabel::deviceIndex
    \brief the index of the paired device

    The Device is the paired PLC.

    The default imageLayerIndex is 0.

    \sa indent
*/
int MyLabel::getDeviceIndex() const
{
    return d->deviceIndex;
}

void MyLabel::setDeviceIndex(int deviceInd)
{
    if(d->deviceIndex == deviceInd)
        return;
    d->deviceIndex = deviceInd;
}

/*!
    \property MyLabel::objectType
    \brief the type of the object

    The object is coil or register of PLC.

    The default objectType is M.

    \sa indent
*/
My::ObjectType MyLabel::getObjectType() const
{
    return d->objectType;
}

void MyLabel::setObjectType(My::ObjectType type)
{
    if(d->objectType == type)
        return;
    d->objectType = type;
}

/*!
    \property MyLabel::objectIndex
    \brief the index of the object

    The default index is 0.

    \sa indent
*/
int MyLabel::getObjectIndex() const
{
    return d->objectIndex;
}

void MyLabel::setObjectIndex(int index)
{
    if(d->objectIndex == index)
        return;
    d->objectIndex = index;
}

/*!
    \property MyLabel::objectData
    \brief the data of the object

    The default objectData is 0.

    \sa indent
*/
double MyLabel::getObjectData() const
{
    return d->objectData;
}

void MyLabel::setObjectData(double data)
{
    if(d->objectData == data)
        return;
    d->objectData = data;
}

void MyLabel::setObjectData(int data)
{
    setObjectData((double)data);
}

/*!
    \property MyLabel::displayEnable
    \brief control the MyLabel whether or not display

    True or False corresponds to display or undisplay.

    The default DisplayEnable is true.

    \sa indent
*/
bool MyLabel::getDisplayEnable() const
{
    return d->displayEnable;
}

void MyLabel::setDisplayEnable(bool displayEnabl)
{
    if(d->displayEnable == displayEnabl)
        return;
    d->displayEnable = displayEnabl;

    if(!d->displayEnable)
    {
        d->textBuffer = text();
        setText(NULL);
    }
    else
    {
        setText(d->textBuffer);
    }
    this->update();
}

/*!
    \property MyLabel::displayDefault
    \brief control the content to display

    True or False corresponds to default or notdefault.

    The default displayDefault is true.

    \sa indent
*/
bool MyLabel::getDisplayDefault() const
{
    return d->displayDefault;
}

void MyLabel::setDisplayDefault(bool displayDefau)
{
    if(d->displayDefault == displayDefau)
        return;
    d->displayDefault = displayDefau;
    this->update();
}

/*!
    \property MyLabel::borderColor
    \brief the color of the border

    The border is the outermost layer of the Label.

    The default border is black.

    \sa indent
*/
QColor MyLabel::getBorderColor() const
{
    return d->borderColor;
}

void MyLabel::setBorderColor(const QColor &borderCol)
{
    if(d->borderColor == borderCol)
        return;
    d->borderColor = borderCol;
    this->update();
}

/*!
    \property MyLabel::borderWidth
    \brief the width of the border

    The border is the outermost layer of the Label.

    The default border is 0.

    \sa indent
*/
int MyLabel::getBorderWidth() const
{
    return d->borderWidth;
}

void MyLabel::setBorderWidth(int borderWid)
{
    if(d->borderWidth == borderWid)
        return;
    d->borderWidth = borderWid;
    this->update();
}

/*!
    \property MyLabel::borderRadius
    \brief the radius of the border

    The border is the outermost layer of the Label.

    The default radius is 0.

    \sa indent
*/
int MyLabel::getBorderRadius() const
{
    return d->borderRadius;
}

void MyLabel::setBorderRadius(int borderRad)
{
    if(d->borderRadius == borderRad)
        return;
    d->borderRadius = borderRad;
    this->update();
}

/*!
    \property MyLabel::myLabelBgColor
    \brief the color of the myLabelBackground

    The border is the outermost layer of the Label.

    The default border is white.

    \sa indent
*/
QColor MyLabel::getMyLabelBgColor() const
{
    return d->myLabelBgColor;
}

void MyLabel::setMyLabelBgColor(const QColor &myLabelBgCol)
{
    if(d->myLabelBgColor == myLabelBgCol)
        return;
    d->myLabelBgColor = myLabelBgCol;
    this->update();
}

/*!
    \reimp
*/
void MyLabel::paintEvent(QPaintEvent *ev)
{
    //绘制准备工作,启用反锯齿
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    //绘制边框
    drawBorder(&painter);
    //绘制背景
    drawBg(&painter);

    if(!d->displayDefault)
        this->setText(QString::number(d->objectData));
    else
        this->setText(d->defaultText);

    QLabel::paintEvent(ev);
}

/*!
 * \brief MyLabel::drawBorder
 * \param painter
 */
void MyLabel::drawBorder(QPainter *painter)
{
    painter->save();

    painter->setPen(QPen(d->borderColor, d->borderWidth));
    painter->setBrush(Qt::NoBrush);

    QPointF topLeft(0, 0);
    QPointF bottomRight(width(), height());
    QRectF rect(topLeft, bottomRight);

    painter->drawRoundedRect(rect, d->borderRadius, d->borderRadius);

    painter->restore();
}

/*!
 * \brief MyLabel::drawBg
 * \param painter
 */
void MyLabel::drawBg(QPainter *painter)
{
    painter->save();

    QPointF topLeft(this->margin(), this->margin());
    QPointF bottomRight(width() - this->margin(), height() - this->margin());
    QRectF bgRect(topLeft, bottomRight);
    QBrush bgBrush(d->myLabelBgColor);
    painter->setPen(Qt::NoPen);
    painter->setBrush(bgBrush);
    painter->drawRoundedRect(bgRect, d->borderRadius/2, d->borderRadius/2);

    painter->restore();
}

/*!
 * \brief MyLabel::enterEvent
 * \param ev
 */
void MyLabel::enterEvent(QEvent *ev)
{
    if(QEvent::Enter == ev->type())
    {
        QCursor cursor;                           //创建光标对象
        cursor.setShape(Qt::PointingHandCursor);  //设置光标形状(指)
        setCursor(cursor);                        //使用光标
    }
}

/*!
 * \brief MyLabel::leaveEvent
 * \param ev
 */
void MyLabel::leaveEvent(QEvent *ev)
{
    if(QEvent::Leave == ev->type())
    {
        QCursor cursor;                           //创建光标对象
        cursor.setShape(Qt::ArrowCursor);         //设置光标形状(箭头)
        setCursor(cursor);                        //使用光标
    }
}

/*!
 * \brief MyLabel::mousePressEvent
 * \param ev
 */
void MyLabel::mousePressEvent(QMouseEvent *ev)
{
    if(Qt::LeftButton == ev->button())
    {
        d->LeftmousePressed = true;
        d->mousePressPoint = ev->globalPos();

        QCursor cursor;                         //创建光标对象
        cursor.setShape(Qt::ClosedHandCursor);  //设置光标形状(抓)
        setCursor(cursor);                      //使用光标
    }
}

/*!
 * \brief MyLabel::mouseReleaseEvent
 * \param ev
 */
void MyLabel::mouseReleaseEvent(QMouseEvent *ev)
{
    if(Qt::LeftButton == ev->button())
    {
        d->LeftmousePressed = false;

        QCursor cursor;                           //创建光标对象
        cursor.setShape(Qt::PointingHandCursor);  //设置光标形状(指)
        setCursor(cursor);                        //使用光标
    }
}

/*!
 * \brief MyLabel::mouseMoveEvent
 * \param ev
 */
void MyLabel::mouseMoveEvent(QMouseEvent *ev)
{
    // 鼠标左键拖动移动
    if(d->LeftmousePressed)
    {
        double mouseDx = ev->globalPos().x() - d->mousePressPoint.x();
        double mouseDy = ev->globalPos().y() - d->mousePressPoint.y();
        d->mousePressPoint = ev->globalPos();

        double myLabelNewx = this->x() + mouseDx;
        double myLabelNewy = this->y() + mouseDy;

        if(myLabelNewx < 0)
            myLabelNewx = 0;
        if(myLabelNewx + this->width() > QApplication::activeWindow()->width())
            myLabelNewx = QApplication::activeWindow()->width() - this->width();
        if(myLabelNewy < 0)
            myLabelNewy = 0;
        if(myLabelNewy + this->height() > QApplication::activeWindow()->height())
            myLabelNewy = QApplication::activeWindow()->height() - this->height();

        move(myLabelNewx, myLabelNewy);
        update();
    }
}

